package com.ruoyi.framework.web.service;

import cn.xfyun.model.response.TtsResponse;
import cn.xfyun.service.tts.AbstractTtsWebSocketListener;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.framework.config.TtsConfig;
import okhttp3.Response;
import okhttp3.WebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * TTS ( Text to Speech ):语音合成
 */
//@Component
public class TtsClientApp {

    private static Logger logger = LoggerFactory.getLogger(TtsClientApp.class);

    @Resource
    private TtsConfig ttsConfig;

//    private static String filePath = "audio/tts1.mp3";
//    private static String resourcePath = "src/main/resources/";

    public String createMp3(String idSeq,String content,int speed) throws MalformedURLException, SignatureException, UnsupportedEncodingException, FileNotFoundException {
//        MyTtsClient ttsClient = new MyTtsClient.Builder()
//                .signature(ttsConfig.getAppId(), ttsConfig.getApiKey(), ttsConfig.getApiSecret())
//                .build();

        MyTtsClient ttsClient = new MyTtsClient.Builder()
                .signature("b145fc51", "118215a748f119ff6407225b655be31b", "YzdlN2JjNDk4NjdkNzE5ZjQ5Zjk1MzE5")
                .build();

        String filePath = RuoYiConfig.getUploadPath();

        File file = new File(filePath + "/audio/"+idSeq+".mp3");
        BlockingQueue<String> queue = new LinkedBlockingQueue<>(5);
        try {
//            ttsClient.send("你好<break time=\"5000ms\"/>科大,<break time=\"5000ms\"/>你好<break time=\"5000ms\"/>科大", new AbstractTtsWebSocketListener(file) {
            ttsClient.send(content, new AbstractTtsWebSocketListener(file) {
                @Override
                public void onSuccess(byte[] bytes) {
//                    return;
                    logger.info("onSuccess--okkk");
                    try {
//                        Thread.sleep(1000*60);
                        queue.put("ok");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        try {
                            queue.put("err");
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }
                    System.out.println("okkk");
                }

                @Override
                public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
                    System.out.println(throwable.getMessage());
                    logger.info("fail"+throwable.getMessage());
                    try {
                        queue.put("err");
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }

                @Override
                public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
//                    System.out.println(ttsResponse.toString());
//                    logger.info("okkk");
                    logger.info("busfail"+ttsResponse.toString());
                    try {
                        queue.put("err");
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            },speed);

            String queueStr = queue.take();
            if (queueStr!=null) {
                logger.info(queueStr);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
//            System.out.println(e.getMessage());
            logger.error("错误码查询链接：https://www.xfyun.cn/document/error-code");
        }
        return "ok";
    }
}
